package com.bnyy.message.bean.chat;

import com.bnyy.message.bean.Contact;
import com.bnyy.message.bean.ContactUser;
import com.bnyy.message.bean.Message;
import com.bnyy.message.enums.ChatType;
import com.bnyy.message.enums.MessageType;

import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/6
 **/
@Table(name = "message_chat")
public class Chat implements Serializable {

    public interface ColumnName {
        String LOGIN_USER_ID = "login_user_id";
        String TOPPING_TIME = "topping_time";
        String CHAT_ID = "chat_id";
        String CHAT_TYPE = "chat_type";
        String RECIPIENT_ID = "recipient_id";
        String TIME = "time";
    }

    /**
     * 群聊 {loginId}_{senderRoleId}_{recipientId}
     * 私聊 {loginId}_{senderRoleId}_{recipientId}_{recipientRoleId}
     * 其他消息 {loginId}_{senderRoleId}_{{@link MessageType}}
     */
    @Column(name = ColumnName.CHAT_ID, isId = true, autoGen = false)
    private String chatId;

    @Column(name = ColumnName.LOGIN_USER_ID)
    private int loginUserId;

    @Column(name = Message.ColumnName.RECIPIENT_ROLE_ID)
    private int recipientRoleId;

    @Column(name = ColumnName.TOPPING_TIME)
    private long toppingTime;

    /**
     * 针对这个会话的接收人id，非单条消息的接收人
     */
    @Column(name = ColumnName.RECIPIENT_ID)
    private int recipientId = 2;

    @Column(name = ColumnName.CHAT_TYPE)
    private int type;

    @Column(name = ColumnName.TIME)
    private long time;

    private ChatInfo chatInfo;

    /**
     * 为空证明没有初始化过
     * 长度为0则说明没有联系人，则群被解散
     *
     * @return
     */
    private ArrayList<ContactUser> contactUsers;

    public Chat() {
    }

    public Chat(int loginUserId, int groupChatId) {
        this(loginUserId, groupChatId, -1, ChatType.GROUP_CHAT);
    }

    public Chat(int loginUserId, int contactUserId, int recipientRoleId) {
        this(loginUserId, contactUserId, recipientRoleId, ChatType.PRIVATE_CHAT);
    }

    public Chat(int loginUserId, int recipientId, int recipientRoleId, ChatType chatType) {
        setLoginUserId(loginUserId);
        setRecipientId(recipientId);
        setType(chatType.getType());
        setRecipientRoleId(recipientRoleId);
        chatId = loginUserId + "_" + recipientId;
        if (chatType == ChatType.PRIVATE_CHAT) {
            chatId += "_" + recipientRoleId;
        }
    }

    public Chat(int loginUserId, ChatMessage chatMessage) {

        this.time = chatMessage.getTime();
        this.loginUserId = loginUserId;
//        this.recipientRoleId = chatMessage.getRecipientRoleId();
//        this.senderRoleId = chatMessage.getSenderRoleId();

        int senderId = chatMessage.getSenderId();
        String chatId = loginUserId + "";

        boolean isSend = senderId == loginUserId;

        MessageType messageType = chatMessage.getMessageType();
        switch (messageType) {
            case GROUP_CHAT_VERIFICATION:
                setType(ChatType.VERIFICATION.getType());
//                setRecipientId(senderId);
                chatId += "_" + ChatType.VERIFICATION.name();
                break;
            case NOTICE_NEW_FANS:
            case NOTICE_ALARM:
            case NOTICE_LOCATION_AUTHORITY:
            case NOTICE_GOODS:
                setType(messageType.getType());
//                setRecipientId(senderId);
                chatId += "_" + getChatType().name();
                break;
            default:
                if (chatMessage.getGroupChatId() != 0) {
                    setType(ChatType.GROUP_CHAT.getType());
                    setRecipientId(chatMessage.getGroupChatId());
                    chatId += "_" + chatMessage.getGroupChatId();
                } else {
                    if (chatMessage.isStranger()) {
                        setType(ChatType.STRANGER.getType());
                        setRecipientId(senderId);
                        setRecipientRoleId(chatMessage.getSenderRoleId());
                        chatId += loginUserId + "_" + ChatType.STRANGER.name();
                    } else {
                        setType(ChatType.PRIVATE_CHAT.getType());
                        if (isSend) { // 针对这个会话的接收人id，非单条消息的接收人，发送的消息是自己则
                            setRecipientId(chatMessage.getRecipientId());
                            setRecipientRoleId(chatMessage.getRecipientRoleId());
                        } else {
                            setRecipientId(senderId);
                            setRecipientRoleId(chatMessage.getSenderRoleId());
                        }
                        chatId += "_" + getRecipientId() + "_" + getRecipientRoleId();
                    }
                }
                break;
        }
        setChatId(chatId);
    }

    public ChatInfo getChatInfo() {
        return chatInfo;
    }

    public void setChatInfo(ChatInfo chatInfo) {
        this.chatInfo = chatInfo;
    }

    public ArrayList<ContactUser> getContactUsers() {
        return contactUsers;
    }

    public void setContactUsers(ArrayList<ContactUser> contactUsers) {
        this.contactUsers = contactUsers;
    }

    public int getLoginUserId() {
        return loginUserId;
    }

    public void setLoginUserId(int loginUserId) {
        this.loginUserId = loginUserId;
    }

    public long getToppingTime() {
        return toppingTime;
    }

    public void setToppingTime(long toppingTime) {
        this.toppingTime = toppingTime;
    }

    public int getRecipientId() {
        return recipientId;
    }

    public void setRecipientId(int recipientId) {
        this.recipientId = recipientId;
    }

    public String getChatId() {
        return chatId;
    }

    public void setChatId(String chatId) {
        this.chatId = chatId;
    }

    public int getType() {
        return type;
    }

    public void setType(int chatType) {
        this.type = chatType;
    }

    public int getRecipientRoleId() {
        return recipientRoleId;
    }

    public void setRecipientRoleId(int recipientRoleId) {
        this.recipientRoleId = recipientRoleId;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public ChatType getChatType() {
        ChatType chatType = ChatType.getChatType(type);
        if (chatType != null) {
            chatType.setChatId(chatId);
        }
        return chatType;
    }

    public boolean isStrangerChat() {
        return false;
    }
}
